from dataInterface.db.params import CPgSqlParam
from isoc.dbop.CFunction import CFunction
import json
import datetime
t= ['tbl_risk_level','risk_abnormal_login', 'risk_abnormal_login_relation', 'risk_remote_control', 'risk_remote_control_relation', 'risk_weak_pwd', 'risk_weak_pwd_relation', 'tbl_attack_relation', 'tbl_risk_asset_attack_link', 'tbl_event_merge', 'tbl_op_event', 'tbl_ra_op_event_status', 'tbl_risk_asset_attack_link', 'tbl_risk_leve', 'tbl_user_op_event', 'tbl_user_record', 'tbl_event_merge_relation']
def drop_isoc(ti1):
    for part in t:
        sql = "select relname from pg_class where oid in (select inhrelid from pg_inherits where inhparent in (select oid from pg_class where relname='%s')) ORDER BY relname;" %(part)
        results = CFunction.execute(CPgSqlParam(sql))
        print part
        for a in json.loads(results):
            if 'default' in a[0]:
                continue

            tableDateStr = a[0].split("_")[-1]
            if ti1 > int(tableDateStr):
                sql1= "alter table internal_app_isoc.%s detach partition internal_app_isoc.%s;" %(part,a[0])
                sql2 = "drop table internal_app_isoc.%s;" %(a[0])
                r1 = CFunction.execute(CPgSqlParam(sql1))
                r2 = CFunction.execute(CPgSqlParam(sql2))

def drop_soar(ti1):
    t2 = ['artifact','artifact_run_detail','playbook_run_detail']
    for part in t2:
        sql = "select relname from pg_class where oid in (select inhrelid from pg_inherits where inhparent in (select oid from pg_class where relname='%s')) ORDER BY relname;" %(part)
        results = CFunction.execute(CPgSqlParam(sql))
        print part
        for a in json.loads(results):
            if 'default' in a[0]:
                continue

            tableDateStr = a[0].split("_")[-1]
            if ti1 > int(tableDateStr):
                sql1= "alter table internal_app_soarmgr.%s detach partition internal_app_soarmgr.%s;" %(part,a[0])
                sql2 = "drop table internal_app_soarmgr.%s;" %(a[0])
                r1 = CFunction.execute(CPgSqlParam(sql1))
                r2 = CFunction.execute(CPgSqlParam(sql2))




def drop_prejudge(ti1):
    t3 = ['operation_event']
    for part in t3:
        sql = "select relname from pg_class where oid in (select inhrelid from pg_inherits where inhparent in (select oid from pg_class where relname='%s')) ORDER BY relname;" %(part)
        results = CFunction.execute(CPgSqlParam(sql))
        print part
        for a in json.loads(results):
            if 'default' in a[0]:
                continue

            tableDateStr = a[0].split("_")[-1]
            if ti1 > int(tableDateStr):
                sql1= "alter table internal_app_prejudgeengine.%s detach partition internal_app_prejudgeengine.%s;" %(part,a[0])
                sql2 = "drop table internal_app_prejudgeengine.%s;" %(a[0])
                r1 = CFunction.execute(CPgSqlParam(sql1))
                r2 = CFunction.execute(CPgSqlParam(sql2))


if __name__ == '__main__':
    today = datetime.date.today()
    # 给出保留最近多少天的数据
    beginDate = str(today - datetime.timedelta(days=35))
    new = int(beginDate.replace('-',''))
    print new
    #drop_isoc(new)
    drop_soar(new)
    drop_prejudge(new)